<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>
    <br />
    <!-- <MBX level1="商品管理" level2="添加商品"></MBX> -->
    <el-card>
      <el-alert title="错误提示的文案" center :closable="false" type="info">
      </el-alert>
      <br />

      <el-steps :active="active * 1" finish-status="success" align-center>
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <br /><br />

      <el-form :model="addform" :rules="rules">
        <!-- tab栏 -->
        <el-tabs
          tab-position="left"
          v-model="active"
          @tab-click="handleClick"
          :before-leave="beforeLeave"
        >
          <el-tab-pane label="基本信息" name="0">
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="addform.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
              <el-input v-model="addform.goods_price"></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input v-model="addform.goods_weight"></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="goods_number">
              <el-input v-model="addform.goods_number"></el-input>
            </el-form-item>
            <el-form-item label="商品分类">
              <el-cascader
                v-model="current_select_arr"
                :options="cateData"
                :props="defaultProps"
                @change="handleChange"
              ></el-cascader>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品参数" name="1">
            <!-- 渲染动态参数 -->
            <el-form-item   :label="item1.attr_name"  v-for="item1 in manyParams" :key="item1.id">
              <el-checkbox-group v-model="item1.attr_vals">
                <el-checkbox :label="item2" v-for="(item2,index) in item1.attr_vals" :key="index"></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品属性" name="2">
              <el-form-item :label='item.attr_name' v-for="item in onlyParams" :key="item.id"> 
                   <el-input v-model="item.attr_vals"></el-input>
              </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品图片" name="3">
              <el-upload
  action="http://www.ysqorz.top:8888/api/private/v1/upload"
  :headers="headers"
  :on-preview="handlePreview" 
  :on-remove="handleRemove"
  :on-success="handlesuccess"
  :file-list="fileList"
  list-type="picture">  
  <el-button size="small" type="primary">点击上传</el-button>
  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
</el-upload>
          </el-tab-pane>
          <el-tab-pane label="商品内容" name="4">定时任务补偿55</el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
        fileList:[],
        headers:{
            Authorization:sessionStorage.getItem('token') 
        },
      active: 0,
      manyParams: [], //动态
      onlyParams: [], //静态
      addform: {
        goods_name: "",
        goods_price: "",
        goods_weight: "",
        goods_number: "",
       pics:[]
      }, //渲染的商品列表
      current_select_arr:[], 
      //当前选中的级别
      cateData: [], //级联框
      defaultProps: { expandTrigger: "hover", label: "cat_name",children:'children',value:'cat_id' }, //级联框默认设置
      rules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.getGoodsCateList();
  },
  methods: {
      //上传成功
      handlesuccess(res){
             console.log('成功',res) 
             this.addform.pics.push({pic:res.data.tmp_path}) 

      },
      //删除上传
      handleRemove(res){
         console.log('删除',res) 
         const index=this.addform.pics.findIndex(item=>{
               return item.pic===res.response.data.tmp_path
         })
         this.addform.pics.splice(index,1) 
      },
      //图片预览
      handlePreview(res){
          
      },

    async handleClick() {
      console.log(this.active);
      if (this.active === "1") {
        //调取动态参数
        const res = await this.$API.getGoodsParamsApi(
          this.current_cate_id
        );
        console.log("动态传餐", res);
        res.forEach((item) => {
          item.attr_vals =
            item.attr_vals.length > 0 ? item.attr_vals.split(",") : [];
        });
        this.manyParams = res;
      } else if (this.active === "2") {
        //调取静态参数
      const res = await this.$API.getGoodsParamsApi(
          this.current_cate_id,
          "only"
        );
        console.log("静态传餐", res);
        this.onlyParams=res
      }
    },
    //点击级联框
    handleChange() {},
    //tab切换之前做是否阻止到下一步的处理
    beforeLeave(newActive, oldActive) {
        console.log("new", newActive);
        console.log("old", oldActive);
        console.log(this.current_select_arr)
      if (oldActive == 0 && this.current_select_arr.length != 3) {
        this.$message({
          type: "error",
          message: "请选择三级分类之后才能进行下一步哦",
        });
        return false;
      }
    },
    //商品列表的数据
    async getGoodsCateList() {
      const res = await this.$API.getGoodsCateApi();
      console.log(res);
      this.cateData = res;
    },
  },
  computed: {
    current_cate_id() {
      return this.current_select_arr[this.current_select_arr.length - 1];
    },
  },
};
</script>

<style scoped>
::v-deep .el-step__title {
  font-size: 12px !important;
}
</style>
